﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Project.Core.Config.Common.Utils {
    public class AppHelper {

        private static IConfiguration _config;
        private static ILogger<AppHelper> _logger;
        public AppHelper(IConfiguration configuration, ILogger<AppHelper> logger) {
            _config = configuration;
            _logger = logger;
        }

        /// <summary>
        /// 读取指定节点的字符串
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="sessions"></param>
        /// <returns></returns>
        public static string[] ReadAppSettings(string msg, params string[] sessions) { 
            try {
                if (sessions.Any()) {
                    //return _config[string.Join(":", sessions)];
                    return _config.GetSection(string.Join(":", sessions)).GetChildren().Select(x => x.Value).ToArray();
                }
            } catch (Exception ex) {
                _logger.LogError($"获取{msg}配置信息失败：{ex.Message}");
                return null;
            }
            _logger.LogError($"获取{msg}配置信息失败：没有可用数据集");
            return null;
        }

        /// <summary>
        /// 读取实体信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public static List<T> ReadAppSettings<T>(string msg, params string[] session) {
            List<T> list = new List<T>();
            try {
                _config.Bind(string.Join(":", session), list);
                if (list.Any()) {
                    return list;
                }
                _logger.LogError($"获取{msg}配置信息失败：没有可用数据集");
                return null;
            } catch (Exception ex) {
                _logger.LogError($"获取{msg}配置信息失败：{ex.Message}");
                return null;
            }
        }

        /// <summary>
        /// 读取指定节点的字符串
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="sessions"></param>
        /// <returns></returns>
        public static string ReadAppSetting(string msg, params string[] sessions) {
            try {
                if (sessions.Any()) {
                    return _config[string.Join(":", sessions)];
                }
            } catch (Exception ex) {
                _logger.LogError($"获取{msg}配置信息失败：{ex.Message}");
                return "";
            }
            _logger.LogError($"获取{msg}配置信息失败：没有可用数据集");
            return "";
        }

        /// <summary>
        /// 读取实体信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public static T ReadAppSetting<T>(string msg, params string[] session) {
            try {
                T data = _config.GetSection(string.Join(":", session)).Get<T>();
                if (data!=null) {
                    return data;
                }
                _logger.LogError($"获取{msg}配置信息失败：没有可用数据集");
                return default(T);
            } catch (Exception ex) {
                _logger.LogError($"获取{msg}配置信息失败：{ex.Message}");
                return default(T);
            }
        }

    }
}
